package softtech.apps.dangtinraovat;

import softtech.apps.utilities.AlertDialogManager;
import softtech.apps.utilities.SessionManager;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

public class ActivityLogin extends Activity {
	SessionManager session;
	// Email, password edittext
	EditText edPhoneNumber, edPassword;
	// login button
	Button btnLogin;
	// Alert Dialog Manager
	AlertDialogManager alert = new AlertDialogManager();
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_login);
		
		// Session Manager
        session = new SessionManager(getApplicationContext());                        
        // Email, Password input text
        edPhoneNumber = (EditText) findViewById(R.id.edPhoneNumber);
        edPassword = (EditText) findViewById(R.id.edPassword); 
        btnLogin = (Button) findViewById(R.id.btnLogin);
        
        btnLogin.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View arg0) {
				// Get username, password from EditText
				String phone_number = edPhoneNumber.getText().toString();
				String password = edPassword.getText().toString();
				
				// Check if username, password is filled				
				if(phone_number.trim().length() > 0 && password.trim().length() > 0){
					if(phone_number.equals("test") && password.equals("test")){
						session.createLoginSession("LoginSession", phone_number);
						// Staring MainActivity
						Intent i = new Intent(getApplicationContext(), ActivityMain.class);
						startActivity(i);
						finish();
					}else{
						// username / password doesn't match
						alert.showAlertDialog(ActivityLogin.this, "Login failed..", "Username/Password is incorrect", false);
					}				
				}else{
					// user didn't entered username or password
					// Show alert asking him to enter the details
					alert.showAlertDialog(ActivityLogin.this, "Login failed..", "Please enter username and password", false);
				}
				
			}
		});
	}
	
	
}
